package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"
)

//TagesTableControl struct
type TagesTableControl struct {
	base dbOperabase
	Data *dborm.Tagstable
}

//CreateTagesTableControl 初始化几种指针函数
func CreateTagesTableControl() *TagesTableControl {
	content := new(TagesTableControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Tagstable)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.TagName
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Tagstable)
		res.TagName = ""
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Tagstable)
	}
	return content
}

//GetAll 获取全部用户-服务器映射信息
func (_this *TagesTableControl) GetAll() ([]*dborm.Tagstable, error) {
	res := make([]*dborm.Tagstable, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//CreateNew 创建一条记录
func (_this *TagesTableControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Tagstable)
}

//FindByID 根据主键获取一条记录
func (_this *TagesTableControl) FindByID(id string) error {
	return _this.base.FindByID(id)
}

//Save 更新当前条目
func (_this *TagesTableControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *TagesTableControl) Delete() error {
	return _this.base.Delete()
}

//GetByClientName 根据客户机名称查询
func (_this *TagesTableControl) GetByIODevice(iodevid string) ([]*dborm.Tagstable, error) {
	res := make([]*dborm.Tagstable, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "io_device = ?", iodevid)
	return res, err
}

//GetByClientName 根据客户机名称查询
func (_this *TagesTableControl) GetByIODeviceAndLogicEquipment(iodevid, logicid string) ([]*dborm.Tagstable, error) {
	res := make([]*dborm.Tagstable, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "io_device = ?  and logic_equipment = ?", iodevid, logicid)
	return res, err
}
